﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net BLL.TermManager ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Wedn.Net.Model;
using Wedn.Net.Utility;

namespace Wedn.Net.BLL
{
    /// <summary>
    /// Term管理类 TermManager
    /// </summary>
    public partial class TermManager
    {
        #region 常用业务
        /// <summary>
        /// 获取当前标签在指定标签集合中的等级
        /// </summary>
        /// <param name="list">标签集合</param>
        /// <param name="entity">当前标签对象</param>
        /// <returns>等级</returns>
        public int GetTermLevel(IList<Term> list, Term entity)
        {
            int pid = entity.ParentId;
            int level = 0;
            while (pid != 0)
            {
                level++;
                var term = list.FirstOrDefault(t => t.Id == pid);
                if (term != null) pid = term.ParentId;
                else return level;
            }
            return level;
        }
        /// <summary>
        /// 加载层级关系的类别集合
        /// </summary>
        /// <param name="list">原类别集合</param>
        /// <param name="categorys">返回新的集合</param>
        /// <param name="mark">标记</param>
        /// <param name="parentId">父ID 传 0</param>
        public void LoadTierCategoryList(IList<Term> list, IList<Term> categorys, string mark, int parentId)
        {
            foreach (var item in list.Where(c => c.ParentId == parentId))
            {
                string tempName = item.Name;
                if (parentId > 0)
                {
                    int level = GetTermLevel(list, item);
                    var sbd = new StringBuilder();
                    for (int i = 0; i < level; i++)
                        sbd.Append(mark);//(i == 0 ? "└─" : mark);
                    tempName = sbd + tempName;
                }
                categorys.Add(new Term
                {
                    Id = item.Id,
                    Name = tempName,
                    Count = item.Count,
                    Description = item.Description,
                    ParentId = item.ParentId,
                    Slug = item.Slug,
                    Type = item.Type
                });
                LoadTierCategoryList(list, categorys, mark, item.Id);
            }
        }
        /// <summary>
        /// 获取一个类别集合其中一个类别的所有子类别集合
        /// </summary>
        /// <param name="categorys">类别集合</param>
        /// <param name="categoryId">当前类别Id</param>
        /// <returns>子类别集合</returns>
        public IList<Term> GetSubCategorys(IList<Term> categorys, int categoryId)
        {
            IList<Term> subs = new List<Term> { categorys.FirstOrDefault(c => c.Id == categoryId) };
            foreach (var item in categorys.Where(c => c.ParentId == categoryId))
            {
                subs.Add(item);
                GetSubCategorys(categorys, subs, item.Id);
            }
            return subs;
        }
        private void GetSubCategorys(IList<Term> categorys, IList<Term> subsCategorys, int parentId)
        {
            foreach (var item in categorys.Where(c => c.ParentId == parentId))
            {
                subsCategorys.Add(item);
                GetSubCategorys(categorys, subsCategorys, item.Id);
            }
        } 
        #endregion

        /// <summary>
        /// 根据传入委托筛选出对应的数据实体集合
        /// </summary>
        /// <param name="where">筛选条件表达式</param>
        /// <returns>查询到数据实体集合</returns>
        public override IQueryable<Term> Select(Expression<Func<Term, bool>> where)
        {
            IQueryable<Term> res = CacheHelper.GetCache<IQueryable<Term>>("TermList");
            if (res == null)
            {
                res = base.Select(t => true);
                CacheHelper.SetCache("TermList", res);
            }
            return res.Where(where);
        }
    }
}
